﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelFileMonitor.NPOI.Config
{
    public class FileWatcherConfig
    {
        public string RootMonitorPath { get; set; } = "ExcelFiles";
        public List<string> FileExtensions { get; set; } = new List<string> { ".xlsx", ".xls" };
        public int MaxConcurrentProcesses { get; set; } = 5;
        public int RetryAttempts { get; set; } = 3;
        public int RetryDelayMs { get; set; } = 1000;
        public int DebounceIntervalMs { get; set; } = 500;
        public int BufferSize { get; set; } = 8192;
        public bool EnableLogging { get; set; } = true;
        public string LogFilePath { get; set; } = "logs/excel_watcher.log";

        public FileWatcherConfig()
        {
        }

        public void Validate()
        {
            if (string.IsNullOrWhiteSpace(RootMonitorPath))
                throw new ArgumentException("监控根路径不能为空");

            if (FileExtensions == null || FileExtensions.Count == 0)
                throw new ArgumentException("必须指定至少一个文件扩展名");

            if (MaxConcurrentProcesses < 1)
                throw new ArgumentException("最大并发进程数必须大于0");

            if (RetryAttempts < 0)
                throw new ArgumentException("重试次数不能为负数");

            if (DebounceIntervalMs < 100)
                throw new ArgumentException("防抖间隔不能小于100毫秒");

            // 确保监控路径存在
            if (!Directory.Exists(RootMonitorPath))
            {
                try
                {
                    Directory.CreateDirectory(RootMonitorPath);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException($"无法创建监控目录 '{RootMonitorPath}': {ex.Message}");
                }
            }
        }

        public string GetFileFilter()
        {
            return "*.*"; // 监控所有文件，在代码中过滤扩展名
        }
    }
}
